<?php
namespace Hive\Mission;
/**
 * Contact Object. 
 * 
 * This object represents the link between 2 customers.
 * A customer can have multiple contacts.
 * 
 *
 * @author Xavier VILAIN
 * @package customer
 * @property int 	$customerid  	Id of the parent customer
 * @property string $role			Role of the contact
 * @property int 	$partnerid		Id of the contact customer
 * @property bool 	$defaultflag	Default Flag
 */
class Partner extends \Fox\Object {
	
	/**
	 * 
	 * Parents Customer
	 * @var string
	 */
	const LINK_PARENTS = 'parents';
	
	/**
	 * 
	 * Parents Customer
	 * @var string
	 */
	const LINK_MISSION = 'mission';	
	
	/**
	 * Link to Contact Object
	 * @var string
	 */
	const LINK_CONTACT = 'contact';
	
	/**
	 * Reverse link to mission
	 * @var string
	 */	
	const LINK_CANDIDATE_MISSION = 'candidateMission';
	
	/**
	 * Step in the current workflow
	 * @var string
	 */
	const LINK_STEP = 'step';
	
	/**
	 * 
	 * Mission Sold-To
	 * @var string
	 */
	const ROLE_SOLD_TO = 'AG';
	
	/**
	 * 
	 * Mission Company/Account
	 * @var string
	 */
	const ROLE_ACCOUNT = 'CO';
	
	/**
	 * 
	 * Role Candidate
	 * @var string
	 */
	const ROLE_CANDIDATE = 'CL';
	
	/**
	 * 
	 * Mission Responsible 
	 * @var string
	 */
	const ROLE_RESPONSIBLE = 'RP';
		
	/**
	 * 
	 * Default Status for each parnter
	 * @var int
	 */
	const STATUS_DEFAULT = '1';
	
	/**
	 * (non-PHPdoc)
	 * @see \Fox\Object::getData()
	 */
	public function getData()
	{
		return array_merge($this->source()->getData(),parent::getData());
	}
	
	/**
	 * 
	 * @return Object Source Data
	 */
	public function source()
	{
		return \Fox\Dao::load(\Hive\Ressource::className(),$this->partnerid);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::beforeVersionUpdate()
	 */
	protected function beforeCreate() {
		$this->udate = $this->cdate = self::sqlDateTime(time());
		$this->uname = $this->cname = \User::current()->getId();
	}
	
	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::beforeVersionUpdate()
	 */
	protected function beforeUpdate($changed) {
		if($changed) {
			$this->udate = self::sqlDateTime(time());
			$this->uname = \User::current()->getId();
		}
		return $changed;
	}
	
	/**
	 * Load Current Step Link
	 * @return \Hive\Workflow\Step
	 */
	public function step() {
		try {
			return \Fox\Dao::load(\Hive\Workflow\Step::className(),$this->status);
		} catch(\Fox\Object\Exception\NotExist $e) {
			return new \Hive\Workflow\Step();
		}
	}
}
\Fox\Dao::infos(
	Partner::className(),
	new \Fox\Object\Info('mission_partners', \Fox\Databases::DB_APP)
);

// Link to contact
\Fox\Object\Links::add(\Fox\Dao::link(Partner::className(),Partner::LINK_CONTACT)
	->to(\Hive\Ressource::className(), \Hive\Ressource::LINK_MISSIONS)
	->on('partnerid'));